//
//  MessageView.m
//  OrderSeat
//
//  Created by 王威 on 14-1-6.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

#import "AlertWindowView.h"
#import "SVProgressHUD.h"
#import <QuartzCore/QuartzCore.h>

#define BTN_IMAGE_ONE_N   @"boxconfirm_btn1_n.png"
#define BTN_IMAGE_ONE_S   @"boxconfirm_btn1_s.png"

#define BTN_IMAGE_TWO_N   @"boxconfirm_btn2_n.png"
#define BTN_IMAGE_TWO_S   @"boxconfirm_btn2_s.png"

#define WINDOW_HEIGHT 120
#define WINDOW_WIDTH  280
#define BTN_WIDTH   120
#define BTN_HEIGHT  40
#define BTN_MARGIN  10
#define TEXT_FONT   [UIFont boldSystemFontOfSize:18]

@implementation AlertWindowView

- (id)initWithMessage:(NSString*)message delegate:(id<AlertWindowViewDelegate>)delegate buttonTitles:(NSArray*)buttonTitles
{
    CGRect frame = CGRectMake((ScreenWidth-WINDOW_WIDTH)/2, (ScreenHeight-WINDOW_HEIGHT)/2, WINDOW_WIDTH, WINDOW_HEIGHT);
    
    self = [super initWithFrame:CGRectMake(Zero, Zero, ScreenWidth, ScreenHeight)];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        
        _m_delegate = delegate;
        
        _baseView = [[UIView alloc] initWithFrame:self.bounds];
        [_baseView setAlpha:0.0f];
        [self addSubview:_baseView];
        
        _backgroundView = [[UIView alloc] initWithFrame:frame];
        [_backgroundView drawBorder];
        [self addSubview:_backgroundView];
        
        _backImageView = [[UIImageView alloc] initWithFrame:_backgroundView.bounds];
        _backImageView.image = [UIImage imageNamed:@"confirm_bg.png"];
        [_backImageView setUserInteractionEnabled:YES];
        [_backgroundView addSubview:_backImageView];
        
        float btnTop = 30;
        if(buttonTitles.count > 0) {
            float btnLeft  = (_backImageView.width - BTN_WIDTH*buttonTitles.count - BTN_MARGIN)/2;
            btnTop = _backImageView.height-BTN_HEIGHT-10*(buttonTitles.count-1);
            _oneButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _oneButton.frame = CGRectMake(btnLeft, btnTop, BTN_WIDTH, BTN_HEIGHT);
            [_oneButton setBackgroundNormal:BTN_IMAGE_ONE_N andHighlighted:BTN_IMAGE_ONE_S];
            [_oneButton setTitleForNormal:[buttonTitles objectAtIndex:0]];
            _oneButton.tag = 0;
            _oneButton.titleLabel.font = TEXT_FONT;
            [_oneButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_backImageView addSubview:_oneButton];
            
            if(buttonTitles.count > 1) {
                btnLeft = _oneButton.right + BTN_MARGIN;
                _twoButton = [UIButton buttonWithType:UIButtonTypeCustom];
                _twoButton.frame = CGRectMake(btnLeft, btnTop, BTN_WIDTH, BTN_HEIGHT);
                [_twoButton setBackgroundNormal:BTN_IMAGE_TWO_N andHighlighted:BTN_IMAGE_TWO_S];
                [_twoButton setTitleForNormal:[buttonTitles objectAtIndex:1]];
                _twoButton.titleLabel.font = TEXT_FONT;
                _twoButton.tag = 1;
                [_twoButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
                [_backImageView addSubview:_twoButton];
            }
        }
        
        float height = [message sizeWithFont:TEXT_FONT].height;
        float top = (btnTop - height)/2;
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, top, _backImageView.width, height)];
        titleLabel.font = TEXT_FONT;
        titleLabel.textColor = [UIColor redColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = message;
        [_backImageView addSubview:titleLabel];
    }
    
    return self;
}

- (void)buttonClick:(id)sender
{
    if ([_m_delegate respondsToSelector:@selector(alertWindow:clickedButtonAtIndex:)]) {
        [_m_delegate alertWindow:self clickedButtonAtIndex:[sender tag]];
    }
    
    [self closeWindow];
}

- (void)showWindow
{
    if ([_m_delegate respondsToSelector:@selector(willShowWindow:)]) {
        [_m_delegate willShowWindow:self];
    }
    self.hidden = NO;
}

- (void)closeWindow
{
    if ([_m_delegate respondsToSelector:@selector(willCloseWindow:)]) {
        [_m_delegate willCloseWindow:self];
    }
    [self removeFromSuperview];
}

@end